<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class Comments extends AuthBase
{
    //评价人力资源公司
    public function handlerComment(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        //$type = input("type", 0, "intval");//2用工方评价人力资源公司 //3工人评价人力资源公司
        //$associated_id = input("associated_id", 0, "intval");//人力资源公司ID
        //$work_id = input("work_id", 0, "intval");
        //$work_rob_id = input("work_rob_id", 0, "intval");//type=3时必须

        $type = $this->request->param("type", 0, "intval");//2用工方评价人力资源公司 //3工人评价人力资源公司
        $associated_id = $this->request->param("associated_id", 0, "intval");//人力资源公司ID
        $work_id = $this->request->param("work_id", 0, "intval");
        $work_rob_id = $this->request->param("work_rob_id", 0, "intval");//type=3时必须

        $comments = $this->request->param("comments", "", "trim");
        $score = $this->request->param("score", "", "trim");

        $data = [
            'type'=>$type,
            'associated_id'=>$associated_id,
            'work_id'=>$work_id,
            'work_rob_id'=>$work_rob_id,
            'comments'=>$comments,
            'score'=>$score,
        ];

        $commentsBis = new \app\common\business\Comments();
        try {
            $comment = $commentsBis->handlerCommentDepartment($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$comment) {
            return Show::error([], "评价失败");
        }

        return Show::success([], "评价成功");
    }

    //工人查看所受评价
    public function getMyListByUserId(){
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");

        //筛选条件
        $where = [
            ['associated_id','=',$this->userId],
            ['type','=',1],
        ];
        $commentsBis = new \app\common\business\Comments();
        $list = $commentsBis->getListByWhere($where,$page,$limit);
        $total=$commentsBis->getCountByWhere($where,'id');

        if (!empty($list)) {
            //关联展示评价者用户信息
            $list = $commentsBis->resetTextList($list);//reset
        }

        return Show::success(['list'=>$list,'count'=>$total], "OK");
    }

    //工人查看所受评价
    public function getListByUserId(){
        $user_id = input("user_id", 0, "intval");
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");

        //筛选条件
        $where = [
            ['associated_id','=',$user_id],
            ['type','=',1],
        ];
        $commentsBis = new \app\common\business\Comments();
        $list = $commentsBis->getListByWhere($where,$page,$limit);
        $total=$commentsBis->getCountByWhere($where,'id');

        if (!empty($list)) {
            //关联展示评价者用户信息
            $list = $commentsBis->resetTextList($list);//reset
        }
        return Show::success(['list'=>$list,'count'=>$total], "OK");
    }
}